package com.progark.sound.view;

import sheep.graphics.Font;
import sheep.graphics.Image;
import sheep.gui.TextButton;
import sheep.input.TouchListener;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.view.MotionEvent;
import com.progark.sound.R;
import com.progark.sound.controller.GameController;
import com.progark.sound.main.Constants;
import com.progark.sound.model.Task;

public class GameView implements TouchListener{
	
	private GameController gameController;
	private Image playButton, hintButton, wrongImage, correctImage;
	private Canvas canvas;
	private TextButton alternative1Button, alternative2Button, alternative3Button, alternative4Button;
	private Task task;
	private Font font = new Font(255, 255, 255, 25, Typeface.DEFAULT_BOLD, Typeface.BOLD);
	private boolean hintClicked, correct, wrong;
	
	public GameView(GameController gameController){
		this.gameController = gameController;
		playButton = new Image(R.drawable.playbutton);
		hintButton = new Image(R.drawable.questionmark);
		wrongImage = new Image(R.drawable.mexicanincorrect);
		correctImage = new Image(R.drawable.mexicancorrect);
		hintClicked = false;
	}

	public void playSound() {
		gameController.getModel().getCurrentTask().getSound().play();
	}
	
	public void draw(Canvas canv){
		this.canvas = canv;
		canvas.drawPaint(sheep.graphics.Color.BLACK);
		playButton.draw(canvas, 1, 1);
		hintButton.draw(canvas, (Constants.WIDTH/2 + Constants.WIDTH/5) , (Constants.HEIGHT/2 + Constants.HEIGHT/5));
		drawAlternatives();
		drawText();
		Image hint = gameController.getModel().getCurrentTask().getHint();
		if (hintClicked)
			hint.draw(canv, 50, 50);
		if (correct)
			correctImage.draw(canvas, 50,50);
		if (wrong)
			wrongImage.draw(canvas, 50, 50);
	}
	//200>750
	//850,950,1050
	private void drawAlternatives(){
		task = gameController.getModel().getCurrentTask();
		alternative1Button = new TextButton(Constants.WIDTH/15, Constants.HEIGHT/3, task.getAlternative1().getText(),Constants.alternativePaint);
		alternative2Button = new TextButton(Constants.WIDTH/15, (Constants.HEIGHT/3 + 75), task.getAlternative2().getText(),Constants.alternativePaint);
		alternative3Button = new TextButton(Constants.WIDTH/15, (Constants.HEIGHT/3 + 150), task.getAlternative3().getText(),Constants.alternativePaint);
		alternative4Button = new TextButton(Constants.WIDTH/15, (Constants.HEIGHT/3 + 225), task.getAlternative4().getText(),Constants.alternativePaint);
		alternative1Button.draw(canvas);
		alternative2Button.draw(canvas);
		alternative3Button.draw(canvas);
		alternative4Button.draw(canvas);
	}
	
	private void drawText(){
		canvas.drawText("Current player: " + gameController.getModel().getCurrentPlayer().toString(), (Constants.WIDTH/3) , 26, font);
		canvas.drawText("Task points: " + Integer.toString(gameController.getModel().getCurrentScore()), (Constants.WIDTH/3), 76, font);
		canvas.drawText("Player 1: " + Integer.toString(gameController.getModel().getPlayer1().getScore()), (Constants.WIDTH/3), 126, font);
		canvas.drawText("Player 2: " + Integer.toString(gameController.getModel().getPlayer2().getScore()), (Constants.WIDTH/3), 176, font);
		
	}

	@Override
	public boolean onTouchDown(MotionEvent event) {
		if(playButton.getBoundingBox().contains(event.getX(), event.getY())){
			playSound();			
		} else if (alternative1Button.onTouchDown(event)){
			checkAnswer(1);
		} else if (alternative2Button.onTouchDown(event)){
			checkAnswer(2);
		} else if (alternative3Button.onTouchDown(event)){
			checkAnswer(3);
		} else if (alternative4Button.onTouchDown(event)){
			checkAnswer(4);
		} else if (event.getX() > (Constants.WIDTH/2 + Constants.WIDTH/5) && event.getY() > (Constants.HEIGHT/2 + Constants.HEIGHT/5)){
			hintClicked = true;
			gameController.getModel().hintUsed();
			long time = System.currentTimeMillis();
			while(System.currentTimeMillis()-time < 500){
				
			}
			hintClicked = false;
		
		}
		return true;
	}


	private void checkAnswer(int i) {
		long time = System.currentTimeMillis();
		 if(task.getAltList().get(i-1).isCorrect()){
			 gameController.getModel().correctAnswer();
			 correct = true;
			 while(System.currentTimeMillis() - time < 1000){
				 
			 }
			 correct = false;
			 
		 } else {
			 gameController.getModel().wrongAnswer();
			 wrong = true;
			 while(System.currentTimeMillis() - time < 1000){
				 
			 }
			 wrong = false;
		 }
	}

	@Override
	public boolean onTouchUp(MotionEvent event) {
		return false;
	}

	@Override
	public boolean onTouchMove(MotionEvent event) {
		return false;
	}
	

}
